/**
 * 战场
 * 战场会以当前场景为初始化
 */
class Battle {
	constructor(scene, enemies) {
		this.scene = scene;
		this.enemies = enemies;
		this.skills = [];
		
		// 新建碰撞引擎
		const engine = Matter.Engine.create();
		const render = Matter.Render.create({ element: document.body, engine:engine, options: {
            showVelocity: true,
            showAngleIndicator: true,
			width:480,
			height:480
        }});
		
		engine.gravity.y = 0;
		engine.gravity.x = 0;
		
		// 将碰撞物体添加到引擎内
		const bodies = [];
		for(var i = 0; i < scene.children.length; i++) {
			const child = scene.children[i];
			if(child.isCylinder && child.constructor.name=="Character") {
				const cpts = child.centerPoint;
				child.body = Matter.Bodies.circle(cpts.x, cpts.y, child.size/4, {isStatic:false, mass:child.weight});
				bodies.push(child.body);
			}
		}
		
		// 将阻挡物设置为静态
		for(var i = 0; i < scene.blocks.length; i++) {
			const block = scene.blocks[i];
			bodies.push(Matter.Bodies.rectangle(block.x+block.width/2, block.y+block.height/2, block.width, block.height, {isStatic:true}));
		}
		
		Matter.Composite.add(engine.world, bodies);// 将所有物体添加到世界中			
		Matter.Render.run(render);//运行渲染器
		var runner = Matter.Runner.create();
		Matter.Runner.run(runner, engine);
		
		Matter.Render.lookAt(render, {
			min: { x: 0, y: 0 },
			max: { x: 480, y: 480 }
		});
		
		// 新建箭头
		const arrow = new PIXI.smooth.SmoothGraphics();
		arrow.beginFill(0xe1de81, 1);
		arrow.drawCircle(0, -30, 3);
		arrow.drawCircle(0, -42, 3);
		arrow.drawCircle(0, -54, 3);
		arrow.moveTo(0, -68).lineTo(-15, -64).lineTo(-13, -62).lineTo(0, -65).
			lineTo(13, -62).lineTo(15, -64).lineTo(0, -68);
		arrow.isCylinder = true;
		arrow.centerPoint = {x:arrow.x,y:arrow.y};
		
		// 添加碰撞事件
		Matter.Events.on(engine, 'collisionStart', function(e) {
			// 读取碰撞块
			for(var i = 0; i < e.pairs.length; i++) {
				const pair = e.pairs[i];
				const bodyA = pair.bodyA;
				const bodyB = pair.bodyB;
				
				if(bodyA.from.constructor.name == "AttackEffect") {
					
				} 
			}
		});

		// 将箭头加入场景
		this.scene.addChild(arrow);
		this.arrow = arrow;
	}
	
	/**
	 * 向战场添加技能
	 */
	skill(skill) {
		this.skills.push(skill);
	}
	
	/**
	 * 更新回合
	 * 再更新回合时会处理用户输入和游戏实体的AI
	 */
	update() {
		// 同步更新body
		for(var i = 0; i < scene.children.length; i++) {
			const child = scene.children[i];
			if(child.isCylinder && child.body) {
				const xDiff = child.body.position.x - child.centerPoint.x;
				const yDiff = child.body.position.y - child.centerPoint.y;
				const distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
				const hude = Math.atan2(xDiff, yDiff);
				updateEnityDirection(child, hude);
				child.state = distance > 2 ? "run":distance < 0.08 ?'idea':"walk";
				child.centerPoint = {x:child.body.position.x,y:child.body.position.y};
			}
		}
		
		// 同步技能
		for(var i = 0; i < this.skills.length; i++) {
			if(!this.skills[i].update()) {
				this.skills.splice(i);
				i--;
			}
		}
		
		// 让箭头跟随角色
		this.arrow.x = this.scene.player.x;
		this.arrow.y = this.scene.player.y - 8;
		
		// 计算箭头方向
		const xDiff = this.arrow.x - this.arrow.centerPoint.x;
		const yDiff = this.arrow.y - this.arrow.centerPoint.y;
		const hude = xDiff == 0 && yDiff == 0 ? 0 : Math.atan2(xDiff, yDiff);
		const angle = 180 - Math.floor((hude * 180 / Math.PI) % 360);
		this.arrow.angle = angle;	
		this.arrow.centerPoint = {x:this.arrow.x,y:this.arrow.y};
		
		// 再进行AI更新,通知所有敌人去攻击主角
		for(var i = 0; i < this.enemies.length; i++)
			this.enemies[i].attackTo(this.scene.player);
	}
	
	/**
	 * 战场结束
	 */
	end() {
		this.scene.removeChild(arrow);
		this.scene.battle = null;
	}
}